package com.liangxy.zhyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liangxy.zhyl.base.PageResponse;
import com.liangxy.zhyl.dto.NursingProjectDto;
import com.liangxy.zhyl.entity.NursingProject;
import com.liangxy.zhyl.mapper.NursingProjectMapper;
import com.liangxy.zhyl.service.NursingProjectService;
import com.liangxy.zhyl.utils.UserThreadLocal;
import com.liangxy.zhyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询护理项目列表
     * @param name     护理项目名称
     * @param status   状态 ： 0 禁用；1 启用
     * @param pageSize 每页数量
     * @param pageNum  页码
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageSize, Integer pageNum) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 查询
        List<NursingProjectVo> nursingProjectList = nursingProjectMapper.getByPage(name, status);
        // 转化为 page 类型
        Page<NursingProjectVo> page = (Page<NursingProjectVo>) nursingProjectList;
        // 获取分页数据
        PageResponse<NursingProjectVo> pageData = PageResponse.of(page, NursingProjectVo.class);
        return pageData;
    }
    /**
     * 新增护理项目
     * @param nursingProjectDTO
     */
    @Override
    public void add(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDTO,nursingProject);
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据id查询护理项目
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDTO) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDTO,nursingProject);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setUpdateBy(UserThreadLocal.getUserId());
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 查询所有的护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> getAll() {
        return nursingProjectMapper.getAll();
    }

    /**
     * 启用/禁用护理项目
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
         nursingProjectMapper.updateStatus(id, status);
    }

    /**
     * 删除护理项目
     * @param id
     */
    @Override
    public void delete(Long id) {
        nursingProjectMapper.deleteById(id);
    }
}
